package com.yunshipei.ui.view.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yunshipei.enterplorer.R;


public class TabView extends LinearLayout implements OnClickListener {

    private OnTabChangeListener mOnTabChangedListener;

    private int mState = -1;

    private final TextView mStateButton1;
    private final TextView mStateButton2;
    private final TextView mStateButton3;
    private final TextView mStateButton4;

    public TabView(Context context) {
        this(context, null);
    }

    public TabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.view_tab, this);
        int padding = getResources().getDimensionPixelOffset(R.dimen.tab_vew_padding_top);
        setPadding(0, padding, 0, padding);

        mStateButton1 = (TextView) findViewById(R.id.button_state1);
        mStateButton2 = (TextView) findViewById(R.id.button_state2);
        mStateButton3 = (TextView) findViewById(R.id.button_state3);
        mStateButton4 = (TextView) findViewById(R.id.button_state4);

        mStateButton1.setOnClickListener(this);
        mStateButton2.setOnClickListener(this);
        mStateButton3.setOnClickListener(this);
        mStateButton4.setOnClickListener(this);
    }

    public void setOnTabChangeListener(OnTabChangeListener listener) {
        mOnTabChangedListener = listener;
    }

    public void setCurrentTab(int index) {
        switchState(index);
    }

    public void buttonClick(int position) {
        switch (position) {
            case 0:
                mStateButton1.performClick();
                break;
            case 1:
                mStateButton2.performClick();
                break;
            case 2:
                mStateButton3.performClick();
                break;
            case 3:
                mStateButton4.performClick();
                break;
        }
    }

    private void switchState(int state) {
        if (mState == state) {
            return;
        } // else continue

        mState = state;
        mStateButton1.setSelected(false);
        mStateButton2.setSelected(false);
        mStateButton3.setSelected(false);
        mStateButton4.setSelected(false);

        switch (mState) {
            case 0:
                mStateButton1.setSelected(true);
                break;

            case 1:
                mStateButton2.setSelected(true);
                break;

            case 2:
                mStateButton3.setSelected(true);
                break;

            case 3:
                mStateButton4.setSelected(true);
                break;

            default:
                break;
        }

        if (mOnTabChangedListener != null) {
            mOnTabChangedListener.onTabChange(mState);
        } // else ignored
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button_state1:
                switchState(0);
                break;

            case R.id.button_state2:
                switchState(1);
                break;

            case R.id.button_state3:
                switchState(2);
                break;

            case R.id.button_state4:
                switchState(3);
                break;

            default:
                break;
        }
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    public interface OnTabChangeListener {
        void onTabChange(int position);
    }
}
